package yunhe.servlet;

import yunhe.entity.UserInfo;
import yunhe.service.UserServers;
import yunhe.service.impl.UserServersImpl;
import yunhe.util.NewsUtil;

import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.IOException;

@SuppressWarnings("serial")
public class UserLogin extends HttpServlet {
	
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	/*	request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");*/
		
		HttpSession session=request.getSession();
		String name = "";
		String pass = "";

		name = request.getParameter("uname");
		pass = request.getParameter("upwd");

		UserServers us = new UserServersImpl();
		int re = us.login(name, pass);


		if (re < 0) {
			session.setAttribute("userLoginState", false);
		} else {
			UserInfo ui = new UserInfo(name);
			session.setAttribute("userInfo", ui);
			Cookie[] cookies1 = { new Cookie("nuName", name),
					new Cookie("nuPass", pass) };
			String s = request.getParameter("select");
			int maxAge = Integer.parseInt(s);
			NewsUtil.addCookies(response, cookies1, maxAge); //登陆成功，写入cookies		
			session.setAttribute("userLoginState", true);
		}
		response.sendRedirect("index.jsp");
	}
	
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}
}
